package com.shop.controller;

import com.alibaba.fastjson.JSON;
import com.shop.entity.GoodType;
import com.shop.entity.ShowPageT;
import com.shop.entity.User;
import com.shop.servlet.GoodTypeServlet;
import com.shop.servlet.impl.GoodTypeServletImpl;
import jakarta.servlet.annotation.WebServlet;

import java.io.IOException;
import java.util.List;

/**
 * com.shop.controller文档说明
 *
 * @author Angluzha
 * @version 2022/9/14
 */
@WebServlet("/goodtype/*")
public class GoodTypeController extends BaseController<GoodType>{
    {
        servlet = new GoodTypeServletImpl();
    }

    public String save(GoodType goodType){
        servlet.insert(goodType);
        return redirect("/goodtype/selectAll");
    }
    public String delete(GoodType goodType){
        servlet.deleteById(goodType.getId());
        return redirect("/goodtype/selectAll");
    }
    public String update(GoodType goodType){
        if (servlet.update(goodType)) {
            return writeWeb("200");
        }else {
            return writeWeb("100");
        }
    }
    public String selectAll(){
        ShowPageT<GoodType> pageT = (ShowPageT<GoodType>) req.getAttribute("pageT");
        page(pageT,"/goodtype/selectAll");
        return forward("/back/goodstype/goodstype.jsp");
    }
    public String delCheck(){
        try {
            String line = req.getReader().readLine();
            List<Integer> userList = JSON.parseArray(line, Integer.class);
            if(servlet.delCheck(userList)){
                return writeWeb("200");
            }else {
                return writeWeb("100");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
